import matplotlib.pyplot as plt
import numpy as np

# 中文显示
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei','DV Sans']
plt.rcParams['axes.unicode_minus'] = False

def bounded_on_closed_interval():
    x = np.linspace(0.1, 2, 1000)
    f = lambda x: 1/x  # 在(0,2]上连续，但在x=0无定义
    
    plt.figure(figsize=(10, 6))
    plt.plot(x, f(x), 'b-', linewidth=2, label='f(x) = 1/x')
    
    # 在闭区间[0.5, 2]上验证有界性
    a, b = 0.5, 2
    x_closed = np.linspace(a, b, 100)
    f_closed = f(x_closed)
    
    upper_bound = max(f_closed)
    lower_bound = min(f_closed)
    
    plt.axvline(x=a, color='r', linestyle='--', label=f'区间[{a},{b}]')
    plt.axvline(x=b, color='r', linestyle='--')
    plt.axhline(y=upper_bound, color='g', linestyle=':', label=f'上界: {upper_bound:.2f}')
    plt.axhline(y=lower_bound, color='g', linestyle=':', label=f'下界: {lower_bound:.2f}')
    
    plt.title('闭区间上连续函数的有界性')
    plt.xlabel('x'); plt.ylabel('f(x)')
    plt.legend(); plt.grid(True, alpha=0.3)
    plt.show()

bounded_on_closed_interval()